﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GenBenEditor
{
    public partial class LayerSelector : UserControl
    {
        public enum Layer : uint
        {
            Background0 = 1,
            Background1 = 2,
            Objects = 4,
            Collision = 8,
            Foreground0 = 16,
            Foreground1 = 32
        }

        public delegate void RepaintMapHandler();
        public event RepaintMapHandler repaint_map;

        public uint draw_layers;
        public uint edit_layer;

        public LayerSelector()
        {
            InitializeComponent();

            draw_layers = (uint)(Layer.Background0 | Layer.Background1 | Layer.Objects 
                | Layer.Foreground0 | Layer.Foreground1);
            edit_layer = (uint)Layer.Background0;
        }

        public bool DrawLayer(int i)
        {
            return (draw_layers & (1 << i)) > 0;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            float y_cursor = 2.0f;
            const float y_advance = 21.0f;
            const float font_y_shift = 4.0f;

            e.Graphics.FillRectangle(Brushes.White, this.ClientRectangle);
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, Width-1, Height-1);

            uint layer_id = 1;
            for (int i = 0; i < 6; ++i)
            {
                if (layer_id == edit_layer)
                    e.Graphics.FillRectangle(Brushes.LightGray, 
                        new Rectangle(1, (int)y_cursor, Width-2, (int)y_advance));

                Layer layer = (Layer)(layer_id);
                e.Graphics.DrawString(layer.ToString(), SystemFonts.DefaultFont,
                    Brushes.Black, new PointF(0.0f, y_cursor + font_y_shift));

                if ((layer_id & draw_layers) > 0)
                    e.Graphics.FillRectangle(Brushes.Black,
                        new Rectangle(100, (int)y_cursor + 3, (int)14, (int)14));
                else
                    e.Graphics.DrawRectangle(Pens.Black,
                        new Rectangle(100, (int)y_cursor + 3, (int)13, (int)13));

                layer_id <<= 1;
                y_cursor += y_advance;
            }
            base.OnPaint(e);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                for (int i = 0; i < 6; ++i)
                {
                    Rectangle draw_box = new Rectangle(100, 5 + i * 21, 14, 14);
                    if (draw_box.Contains(e.Location))
                    {
                        draw_layers ^= (uint)(1 << i);
                        repaint_map();
                        break;
                    }

                    Rectangle edit_box = new Rectangle(1, 2 + i * 21, Width - 2, 21);
                    if (edit_box.Contains(e.Location))
                    {
                        edit_layer = (uint)(1 << i);
                        break;
                    }
                }
                Invalidate();
            }
            base.OnMouseClick(e);
        }
    }
}
